//
//  RJWarningAQICheckDay.m
//  AirQuality
//
//  Created by Po on 15/4/14.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJWarningAQICheckDay.h"
@interface RJWarningAQICheckDay () <UITextFieldDelegate>

@property (assign, nonatomic) BOOL isConnect;
@property (assign, nonatomic) UITextField * connectTextField;
@property (assign, nonatomic) UITextField * currentTextField;

@end

@implementation RJWarningAQICheckDay

- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        NSArray * array = [[NSBundle mainBundle] loadNibNamed:@"RJWarningAQICheckDay" owner:self options:nil];
        UIView * view = array[0];
        [view setFrame:self.bounds];
        [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"alphaBack"]]];
        [self addSubview:view];
        [self setTextField];
        _isConnect = NO;
        
        NSArray * labelArray = @[_timeLabel, _timeLabel, _AQIRangeLabel, _AQICenterLabel, _expertLabel, _mainClassCheckLabel, _firstPM25Label, _firstNoPM25Label,_aqiCenterTittleLabel,_aqiRankTittleLabel, _aqiRankWarningLabel, _mansTittleLabel, _manTittleLabel, _mainClassTittleLabel,_mainClassCheckLabel, _trueTittleLabel, _trueTittleLabel, _lookButtonBackLabel,_trueValueLabel];
        for (UILabel * label  in labelArray) {
            [label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"homeTableCellBack"]]];
        }
        NSArray * textFiledArray = @[_AQIminTextFiled, _AQImaxTextFiled, _AQIcenterTextField, _expertMinTextField, _expertMaxTextField, _mainClassCheckMaxTextField, _mainClassCheckTextField];
        for (UITextField * textFiled in textFiledArray) {
            [textFiled setBackgroundColor:[UIColor clearColor]];
            [textFiled setValue:GetRGBColor(255, 255, 255, 0.5) forKeyPath:@"_placeholderLabel.textColor"];
            [textFiled setBorderStyle:UITextBorderStyleNone];
        }
    }
    return self;
}

- (void)setTextField
{
    [_AQIminTextFiled setDelegate:self];;
    [_AQImaxTextFiled setDelegate:self];
    [_AQIcenterTextField setDelegate:self];
    [_expertMinTextField setDelegate:self];
    [_expertMaxTextField setDelegate:self];
    [_mainClassCheckTextField setDelegate:self];
    [_mainClassCheckMaxTextField setDelegate:self];
}

- (void)setDataEditState:(BOOL)isEdit
{
    [_AQIcenterTextField setHidden:isEdit];
    [_AQImaxTextFiled setHidden:isEdit];
    [_AQIminTextFiled setHidden:isEdit];
    [_mainClassCheckTextField setHidden:isEdit];
    [_mainClassCheckMaxTextField setHidden:isEdit];
}

/**拷贝当前关联Text与最新的字符string*/
- (void)copyConnectTextFiledAndString:(NSString *)string
{
    if (!_isConnect) {
        return;
    }
    NSMutableString * text = [_currentTextField.text mutableCopy];
    if ([string isEqualToString:@""]) {
        [text deleteCharactersInRange:NSMakeRange(text.length - 1, 1)];
    } else {
        [text appendString:string];
    }
    _connectTextField.text = text;
}

#pragma mark - Event
- (IBAction)pressDetailButton:(UIButton *)sender {
    RJDetailTable * detailTabel = [[RJDetailTable alloc] initWithData:_detailData];
    [detailTabel.titleLabel setText:@"AQI范围预报详情"];
    [sysWindow addSubview:detailTabel];
}


#pragma mark - UITextFiledDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (_currentTextField != textField) {
        if (textField == _AQIcenterTextField){
            [_AQIminTextFiled setBackgroundColor:[UIColor clearColor]];
            [_AQImaxTextFiled setBackgroundColor:[UIColor clearColor]];
            [_AQIcenterTextField setBackgroundColor:GetRGBColor(0, 228, 0, 0.5)];
        } else if(textField == _AQIminTextFiled || textField == _AQImaxTextFiled) {
            [_AQIminTextFiled setBackgroundColor:GetRGBColor(0, 228, 0, 0.5)];
            [_AQImaxTextFiled setBackgroundColor:GetRGBColor(0, 228, 0, 0.5)];
            [_AQIcenterTextField setBackgroundColor:[UIColor clearColor]];
        }
        _currentTextField = textField;
    }
    if (textField == _AQIminTextFiled) {
        _isConnect = YES;
        _connectTextField = _expertMinTextField;
    } else if (textField == _AQImaxTextFiled) {
        _connectTextField = _expertMaxTextField;
        _isConnect = YES;
    } else {
        _isConnect = NO;
    }
    CGFloat yPoint = CGRectGetMaxY(textField.frame) + self.frame.origin.y + textField.frame.size.height + 10;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WarningTextShow" object:nil userInfo:@{@"textBottomY":@(yPoint)}];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if ([string isEqualToString:@""]) {
        return YES;
    }
    
    if([textField.text length] >= 3){
        return NO;
    }
    
    NSRange mrange = [textField.text rangeOfString:@"."];
    
    //约束0的个数
    if ([string isEqualToString:@"0"] && mrange.location == NSNotFound && [textField.text floatValue] < 1) {
        NSString * text = [NSString stringWithFormat:@"%@0",textField.text];
        NSNumber * num = @([text floatValue]);
        [textField setText:GetStringWithNum(num)];
        return NO;
    }
    
    if ([string isEqualToString:@"."]) {
        //自动补0
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"0";
            [self copyConnectTextFiledAndString:string];
            return YES;
        }
        //限制小数点
        if (mrange.location != NSNotFound && (range.location != mrange.location)) {
            return NO;
        }
        [self copyConnectTextFiledAndString:string];
        return YES;
    }
    
    //限制输入为数字
    if ([RJTools isNumberString:string] || [string isEqualToString:@""]) {
        [self copyConnectTextFiledAndString:string];
        return YES;
    }
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField endEditing:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WarningTextHide" object:nil userInfo:nil];
    if ((textField == _AQIcenterTextField) && ![textField.text isEqualToString:@""] && _delegate) {
        [_delegate WarningDay:self number:[textField.text floatValue]];
    } else if ([textField.text isEqualToString:@""]) {
        if (textField == _AQIcenterTextField){
            [_AQIminTextFiled setBackgroundColor:GetRGBColor(0, 228, 0, 0.5)];
            [_AQImaxTextFiled setBackgroundColor:GetRGBColor(0, 228, 0, 0.5)];
            [_AQIcenterTextField setBackgroundColor:[UIColor clearColor]];
        } else if(textField == _AQIminTextFiled || textField == _AQImaxTextFiled) {
            [_AQIminTextFiled setBackgroundColor:[UIColor clearColor]];
            [_AQImaxTextFiled setBackgroundColor:[UIColor clearColor]];
            [_AQIcenterTextField setBackgroundColor:GetRGBColor(0, 228, 0, 0.5)];
        }
    }
    

    return YES;
}
@end
